package ReSofting.it.GoldDirector.JsonTrans;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.UUID;

import org.apache.http.entity.StringEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import ReSofting.it.GoldDirector.Model.myItemOrdine;
import ReSofting.it.GoldDirector.Model.myOrdine;
import android.util.Log;

public class JsonTransformer_myOrdine 
{
	/*
	 String Name = pJsObj.getString("");
	 int ID = pJsObj.getInt("");	
	 res = UUID.fromString((String) pJsObj.get("guidmn"));
	 */
	public myOrdine jsonTo_myOrdine(JSONObject pJsObj)
	{
		if(pJsObj == null) return null;
		
		 try {
			 myOrdine res = new myOrdine();
			 
			 res.IDOrd = pJsObj.getInt("id");		 
			 
			 res.Tavolo = pJsObj.getString("tavolo");
			 
			 //res. = pJsObj.getString("orario");
			 res.NCoperti = pJsObj.getInt("ncoperti");
			 res.NClienti = pJsObj.getInt("nclienti");			 
			 res.Note = pJsObj.getString("note");
			 res.Totale = (float) pJsObj.getDouble("Totale");
			 res.NItems = pJsObj.getInt("NItems");
			 
			 res.MenuItems = new ArrayList<myItemOrdine>();
			 
			 //tasformo in array
			 JSONArray jarr = pJsObj.getJSONArray("ItemsOrdini");			 
			 JSONObject jobj = null;
			 
			 myItemOrdine item = null;
			 for(int i = 0; i < jarr.length(); i++) 
			 {
				 jobj = jarr.getJSONObject(i);
				 
				 item = new myItemOrdine();
				 item.IDItOrd = jobj.getInt("IDItOrd") ;
				 item.Nome = jobj.getString("Nome");
				 item.Evaso = jobj.getBoolean("Evaso");
				 item.FkMenu = UUID.fromString( jobj.getString("FkMenu") );
				 item.FkOrdine = jobj.getInt("FkOrdine");
				 item.Note = jobj.getString("Note");
				 item.Tot = jobj.getInt("Tot");
				 
				 res.MenuItems.add(item);
			 }
			 
			 
			 return res ;
		} catch (JSONException e) {
			Log.v("Errore from Json to Obj in  fromJsonToMyOrder ", e.getMessage());
			e.printStackTrace();
			return null;
		}
    	 
	}//fM
	
	public StringEntity fromMyOrdine_ToJson(myOrdine pObj)
	{
		if(pObj == null) return null;
		
		 try {
        	 
        	 JSONArray jsArray = new JSONArray(pObj.MenuItems);
        	 
        	 /*
        	 JSONArray arr = new JSONArray();
        	 JSONObject element = null ;
        	 myMenuItem item = null;
        	 
        	 for (int i = 0; i < _Order.MenuItems.size(); ++i) 
 	        {	        				
        		 element = new JSONObject();
        		 item = _Order.MenuItems.get(i);	
        		 
        		 element.put("guid",item.Guid);
	             element.put("name",item.Name);
	             element.put("tot",item.Tot);
	             
	             arr.put(element);	        		 
 	        }   
			*/
        	 
            JSONStringer ordine = new JSONStringer()
            .object()
                .key("ordine")
                    .object()
                        .key("id").value(0)
                        .key("Tavolo").value(pObj.Tavolo)
                        .key("NClienti").value(pObj.NClienti)
                        .key("NCoperti").value(pObj.NCoperti)
                        .key("NItems").value(pObj.MenuItems.size())
                        .key("Note").value(pObj.Note)
                        .key("itemsordini") .value(jsArray)                       
                        
                        //.endObject()
                    .endObject()
                .endObject();
            
            Log.d("Diego", ordine.toString());
 
            StringEntity dati = new StringEntity(ordine.toString());
            return dati;
			
		 } 
		 catch (UnsupportedEncodingException ex1) {
			 	Log.v("Errore from Json to Obj in  fromMyOrdine_ToJson ", ex1.getMessage());
				ex1.printStackTrace();
				return null;
		}            
		 catch (JSONException ex) {
			Log.v("Errore from Json to Obj in  fromMyOrdine_ToJson ", ex.getMessage());
			ex.printStackTrace();
			return null;
		}
 	catch (Exception e) {
		Log.v("Errore from Json to Obj in  fromMyOrdine_ToJson ", e.getMessage());
		e.printStackTrace();
		return null;
	}
    	 
	}//fM
	
}
